import {uEditUserCompany} from './../../../api/user'

import {observable, computed, action} from 'mobx'
import {createContext} from 'react'
import ChildrenPageUtils from '@/store/extends'
import {uSelectUserInfo} from '@/api/user'
import {getStorage} from '@/utils/localstoreage'
import Taro from '@tarojs/taro'

class Editcompanyname {
  @observable pageData: {
    devCount: number
    user: UserSetting.UserInfo | null
  } = {
    devCount: 0,
    user: null
  }
  @action.bound
  async updatePageDataSource() {
    let userId = getStorage('userId')
    if (!userId) return
    let res = await uSelectUserInfo({userId})
    if (res && res.result === 'success') {
      this.pageData = res.data
    } else {
      this.pageData = {
        devCount: 0,
        user: null
      }
    }
  }

  @action.bound
  async formSubmit({companyName}: {companyName: string}) {
    let userId = getStorage('userId')
    if (!userId) return
    let res = await uEditUserCompany({userId, userCompany: companyName})
    if (res) {
      if (res.result === 'success') {
        Taro.showToast({
          icon: 'success',
          title: '修改成功'
        })
      } else if (res.result === 'error' && res.msg) {
        Taro.showToast({
          icon: 'error',
          title: res.msg
        })
      }
    }
  }
}
export const editCompanyNameStore = createContext(new Editcompanyname())
